//
//  KLMessageViewController.m
//  KunLun
//
//  Created by apple on 2017/11/30.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "KLMessageChatViewController.h"

@interface KLMessageChatViewController ()<EaseMessageViewControllerDelegate,EaseMessageViewControllerDataSource>

@end

@implementation KLMessageChatViewController
+ (void)load{
    //更换方法
    Method originalMethod = class_getInstanceMethod([super class], @selector(_sendMessage:isNeedUploadFile:));
    Method swizzledMethod = class_getInstanceMethod([self class], @selector(s_sendMessage:isNeedUploadFile:));
    method_exchangeImplementations(originalMethod, swizzledMethod);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    self.dataSource = self;
    if (!self.title.length) {
         self.title = [self findChatrTitle];
    }
    self.title = @"黄金口袋在线客服";
    // Do any additional setup after loading the view.
}
- (NSString*)findChatrTitle{
    EMMessage * message = self.conversation.lastReceivedMessage;
    if (message) {
        if ([message.ext.allKeys containsObject:@"name"]) {
            return message.ext[@"name"];
        }
    }
    return self.conversation.conversationId;
    
}
- (void)s_sendMessage:(EMMessage *)message
    isNeedUploadFile:(BOOL)isUploadFile{
    if (self.conversation.type == EMConversationTypeGroupChat){
        message.chatType = EMChatTypeGroupChat;
    }
    else if (self.conversation.type == EMConversationTypeChatRoom){
        message.chatType = EMChatTypeChatRoom;
    }
    //添加扩展消息
    NSString * name = [STUserManger defult].loginUser.nickname.length?[STUserManger defult].loginUser.nickname:@"";
    NSString * iconUrl = [STUserManger defult].loginUser.head.length?[STUserManger defult].loginUser.head:@"";
    NSString * userID  = [STUserManger defult].userID.length?[STUserManger defult].userID:@"";
    NSDictionary * dic = @{@"name":name,@"iconUrl":iconUrl,@"":userID};
    message.ext = dic;


    __weak typeof(self) weakself = self;
    if (!([EMClient sharedClient].options.isAutoTransferMessageAttachments) && isUploadFile) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:NSLocalizedString(@"message.autoTransfer", @"Please customize the transfer attachment method") delegate:nil cancelButtonTitle:NSLocalizedString(@"sure", @"OK") otherButtonTitles:nil, nil];
        [alertView show];
    } else {
        [self addMessageToDataSource:message
                            progress:nil];
        
        [[EMClient sharedClient].chatManager sendMessage:message progress:^(int progress) {
            if (weakself.dataSource && [weakself.dataSource respondsToSelector:@selector(messageViewController:updateProgress:messageModel:messageBody:)]) {
                [weakself.dataSource messageViewController:weakself updateProgress:progress messageModel:nil messageBody:message.body];
            }
        } completion:^(EMMessage *aMessage, EMError *aError) {
            if (!aError) {
                [weakself performSelector:@selector(_refreshAfterSentMessage:) withObject:aMessage];
                //[weakself _refreshAfterSentMessage:aMessage];
            }
            else {
                [weakself.tableView reloadData];
            }
        }];
    }
}

- (id<IMessageModel>)messageViewController:(EaseMessageViewController *)viewController modelForMessage:(EMMessage *)message{
    
    id <IMessageModel> obj = nil;
    obj = [[EaseMessageModel alloc] initWithMessage:message];
//    NSString * userID  = [STUserManger defult].userID.length?[STUserManger defult].userID:@"";
    if (obj.isSender) {
        NSString * name = [STUserManger defult].loginUser.nickname.length?[STUserManger defult].loginUser.nickname:@"";
        NSString * iconUrl = [STUserManger defult].loginUser.head.length?[STUserManger defult].loginUser.head:@"";
        obj.nickname = name;
        if(iconUrl.length){
             obj.avatarURLPath = iconUrl;
            
        }else{
            UIImage * image = [UIImage imageNamed:@"touxiang"];
            //logo_release touxiang
            obj.avatarImage = image;
        }
       
    
    }else{
        if (self.customServieceName.length) {
            obj.nickname =self.customServieceName;
        }else{
           obj.nickname =@"黄金口袋客服";
        }
        obj.avatarImage = [UIImage imageNamed:@"logo_release"];
        if ([message.ext.allKeys containsObject:@"name"]) {
            obj.nickname = message.ext[@"name"];
        }
        if ([message.ext.allKeys containsObject:@"iconUrl"]) {
            obj.avatarURLPath = message.ext[@"iconUrl"];
        }
        
    }
    return obj;
}

@end
